import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:speed_random/app_styles.dart';
import 'package:speed_random/core/config/app_config.dart';
import 'package:speed_random/core/services/ad_service.dart';

import 'logic.dart';
import 'state.dart';

class SettingsPage extends StatelessWidget {
  final RandNumLogic logic;
  final RandNumState state;

  const SettingsPage({Key? key, required this.logic, required this.state}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    AppConfig.current.adService.showAd(adType: AdType.interstitial);

    final minController = TextEditingController(text: state.min.value.toString());
    final maxController = TextEditingController(text: state.max.value.toString());
    final countController = TextEditingController(text: state.count.value.toString());
    // 新增滚动时间控制器
    final scrollTimeController = TextEditingController(text: '3');
    // 新增手动停止开关状态
    final isManualStop = state.isManualStop.value.obs;
    // 新增声音提示开关状态
    final isSoundEnabled = state.isSoundEnabled.value.obs;
    // 新增震动提示开关状态
    final isVibrationEnabled = state.isVibrationEnabled.value.obs;

    return Scaffold(
      appBar: AppBar(
        title: Text('设置'.tr),
        backgroundColor: AppStyles.colorAppBarbg,
        centerTitle: true,
        elevation: 0,
      ),
      body: SingleChildScrollView(
        padding: EdgeInsets.all(16),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            // 输入框组
            Card(
              elevation: 1,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(12),
              ),
              child: Padding(
                padding: EdgeInsets.all(16),
                child: Column(
                  children: [
                    TextField(
                      controller: minController,
                      keyboardType: TextInputType.number,
                      decoration: InputDecoration(
                        labelText: '最小值'.tr+"(0)",
                        border: OutlineInputBorder(),
                        contentPadding: EdgeInsets.symmetric(horizontal: 12, vertical: 14),
                      ),
                    ),
                    SizedBox(height: 16),
                    TextField(
                      controller: maxController,
                      keyboardType: TextInputType.number,
                      decoration: InputDecoration(
                        labelText: '最大值'.tr+"(1-99999)",
                        border: OutlineInputBorder(),
                        contentPadding: EdgeInsets.symmetric(horizontal: 12, vertical: 14),
                      ),
                    ),
                    SizedBox(height: 16),
                    TextField(
                      controller: countController,
                      keyboardType: TextInputType.number,
                      decoration: InputDecoration(
                        labelText: '生成结果数量'.tr+"(1-100)",
                        border: OutlineInputBorder(),
                        contentPadding: EdgeInsets.symmetric(horizontal: 12, vertical: 14),
                      ),
                    ),
                    SizedBox(height: 16),
                    TextField(
                      controller: scrollTimeController,
                      keyboardType: TextInputType.number,
                      decoration: InputDecoration(
                        labelText: '滚动时间(秒)'.tr+"(1-9)",
                        border: OutlineInputBorder(),
                        contentPadding: EdgeInsets.symmetric(horizontal: 12, vertical: 14),
                      ),
                    ),
                  ],
                ),
              ),
            ),

            SizedBox(height: 16),


            // 开关选项组
            Card(
              elevation: 1,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(12),
              ),
              child: Padding(
                padding: EdgeInsets.all(16),
                child: Column(
                  children: [
                    _buildSwitchRow('结果是否重复'.tr, state.isRepeatable, (value) => logic.setRepeatable(value)),
                    Divider(height: 24),
                    _buildSwitchRow('总和'.tr, state.isShowSum, (value) => state.isShowSum.value = value),
                    Divider(height: 24),
                    _buildSwitchRow('位数补0'.tr, state.isadd0, (value) => state.isadd0.value = value),
                    Divider(height: 24),
                    _buildSwitchRow('手动停止滚动'.tr, isManualStop, (value) => isManualStop.value = value),
                    Divider(height: 24),
                    _buildSwitchRow('声音提示'.tr, isSoundEnabled, (value) => isSoundEnabled.value = value),
                    Divider(height: 24),
                    _buildSwitchRow('震动提示'.tr, isVibrationEnabled, (value) => isVibrationEnabled.value = value),
                  ],
                ),
              ),
            ),

            SizedBox(height: 24),

            // 按钮组
            Row(
              children: [
                Expanded(
                  child: OutlinedButton(
                    onPressed: () => Navigator.pop(context),
                    style: OutlinedButton.styleFrom(
                      padding: EdgeInsets.symmetric(vertical: 16),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(8),
                      ),
                    ),
                    child: Text('取消'.tr),
                  ),
                ),
                SizedBox(width: 16),
                Expanded(
                  child: ElevatedButton(
                    onPressed: () {
                      // ... 保存逻辑保持不变 ...
                      // 处理滚动时间输入
                      int scrollTime = (int.tryParse(scrollTimeController.text) ?? 3).clamp(1, 9);
                      logic.setSave(
                          int.tryParse(minController.text) ?? 0,
                          int.tryParse(maxController.text) ?? 100,
                          int.tryParse(countController.text) ?? 1,
                          state.isRepeatable.value,
                          scrollTime,
                          isManualStop.value,
                          isSoundEnabled.value,
                          isVibrationEnabled.value
                      );
                      Navigator.pop(context);
                    },
                    style: ElevatedButton.styleFrom(
                      padding: EdgeInsets.symmetric(vertical: 16),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(8),
                      ),
                    ),
                    child: Text('保存'.tr),
                  ),
                ),
              ],
            ),
            SizedBox(height: 24),
          ],
        ),
      ),
    );
  }

  Widget _buildSwitchRow(String title, RxBool value, ValueChanged<bool> onChanged) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(title, style: TextStyle(fontSize: 16)),
        Obx(() => Switch(
          value: value.value,
          onChanged: onChanged,
        )),
      ],
    );
  }
}

